package com.smyh.util;


public class HexStrUtil {
	/**
	 * 字符串转换成十六进制字符串
	 */

	public static String str2HexStr(String str) {

		char[] chars = "0123456789ABCDEF".toCharArray();

		StringBuilder sb = new StringBuilder("");

		byte[] bs = str.getBytes();

		int bit;

		for (int i = 0; i < bs.length; i++) {

			bit = (bs[i] & 0x0f0) >> 4;

			sb.append(chars[bit]);

			bit = bs[i] & 0x0f;

			sb.append(chars[bit]);

		}

		return sb.toString();

	}

	/**
	 * 
	 * 十六进制转换字符串
	 * 
	 */

	public static String hexStr2Str(String hexStr) {

		String str = "0123456789ABCDEF";

		char[] hexs = hexStr.toCharArray();

		byte[] bytes = new byte[hexStr.length() / 2];

		int n;

		for (int i = 0; i < bytes.length; i++) {

			n = str.indexOf(hexs[2 * i]) * 16;

			n += str.indexOf(hexs[2 * i + 1]);

			bytes[i] = (byte) (n & 0xff);

		}

		return new String(bytes);

	}

	/**
	 * bytes转换成十六进制字符串
	 */
	public static String byte2HexStr(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
			// if (n<b.length-1) hs=hs+":";
		}
		return hs.toUpperCase();
	}

	private static byte uniteBytes(String src0, String src1) {
		byte b0 = Byte.decode("0x" + src0).byteValue();
		b0 = (byte) (b0 << 4);
		byte b1 = Byte.decode("0x" + src1).byteValue();
		byte ret = (byte) (b0 | b1);
		return ret;
	}

	/**
	 * 十六进制字符串转换成bytes
	 */
	public static byte[] hexStr2Bytes(String src) {
		if(src == null || "null".equals(src)){
			return new byte[1];
		}
		src = src.replaceAll("\\s", "");
		int m = 0, n = 0;
		int l = src.length() / 2;
		if(src.length()%2 != 0){
			src = "0"+src;
			l++;
		}
//		System.out.println(l);
		byte[] ret = new byte[l];
		for (int i = 0; i < l; i++) {
			m = i * 2 + 1;
			n = m + 1;
			ret[i] = uniteBytes(src.substring(i * 2, m), src.substring(m, n));
		}
		return ret;
	}

	/**
	 * String 的字符串转换成 unicode 的String
	 */
	public static String stringToUnicode(String strText) throws Exception {
		char c;
		String strRet = "";
		int intAsc;
		String strHex;
		for (int i = 0; i < strText.length(); i++) {
			c = strText.charAt(i);
			intAsc = c;
			strHex = Integer.toHexString(intAsc);
			if (intAsc > 128) {
				strRet += "\\u" + strHex;
			} else {
				// 低位在前面补00
				strRet += "\\u00" + strHex;
			}
		}
		return strRet;
	}

	/**
	 * unicode的String转换成String的字符串
	 */
	public static String unicodeToString(String hex) {
		int t = hex.length() / 6;
		StringBuilder str = new StringBuilder();
		for (int i = 0; i < t; i++) {
			String s = hex.substring(i * 6, (i + 1) * 6);
			// 高位需要补上00再转
			String s1 = s.substring(2, 4) + "00";
			// 低位直接转
			String s2 = s.substring(4);
			// 将16进制的string转为int
			int n = Integer.valueOf(s1, 16) + Integer.valueOf(s2, 16);
			// 将int转换为字符
			char[] chars = Character.toChars(n);
			str.append(new String(chars));
		}
		return str.toString();
	}
	
	/**
	 * 功能：把一个长整型参数，安装字节长度转换多个字节返回字节数组
	 * 作者：zhuchunsheng
	 * 参数：@param srcNum 元数据
	 * 参数：@param byteSize 字符串长度
	 * 参数：@param isH2L 是否-高到低
	 * 参数：@return
	 * 返回值：byte[]
	 * 日期时间：2013-7-19 上午8:58:36
	 */
	public static byte[] num2Bytes(long srcNum, int byteSize,boolean isH2L){
		byte[] byteResult = new byte[byteSize];
		for(int i=0; i<byteSize; i++){
			if(isH2L){
				//高字节-低字节
				byteResult[i] = (byte) (srcNum>>(8*(byteSize-1-i)));
			}else{
				//低字节-高字节
				byteResult[i] = (byte) (srcNum>>(8*i));
			}
		}
		
		return byteResult;
	}
	
	/**
	 * 功能：对象数组反转
	 * 作者：zhuchunsheng
	 * 参数：@param srcArr
	 * 返回值：void
	 * 日期时间：2013-7-24 下午4:39:55
	 */
	public static void arraysReveral(Object[] srcArr){
		Object tempObj = null;
		int length = srcArr.length;
		for(int i=0; i<length/2; i++){
			tempObj = srcArr[i];
			srcArr[i] = srcArr[length-i-1];
			srcArr[length-i-1] = tempObj;
		}
	}
	
	/**
	 * 功能：字节数组反转
	 * 作者：zhuchunsheng
	 * 参数：@param srcArr
	 * 返回值：void
	 * 日期时间：2013-7-24 下午4:39:55
	 */
	public static void arraysReveral(byte[] srcArr){
		byte tempObj ;
		int length = srcArr.length;
		for(int i=0; i<length/2; i++){
			tempObj = srcArr[i];
			srcArr[i] = srcArr[length-i-1];
			srcArr[length-i-1] = tempObj;
		}
	}
	
}
